#include <string>
#include "atomic.h"
#include "message.h"
#include "mainsimu.h"
#include "strutil.h"
#include "../HeadFile/MES_CDU_processing_2_model.h"


/**** the Constructor ****/
MES_CDU_processing_2_model::MES_CDU_processing_2_model( const string &name)
: Atomic( name )
, MES_CDU_2_stream_mode( this->addInputPort( "MES_CDU_2_stream_mode") )
, MES_CDU_2_stream_crude( this->addInputPort( "MES_CDU_2_stream_crude") )
, MES_CDU_2_stream_VR( this->addOutputPort( "MES_CDU_2_stream_VR") )
, MES_CDU_2_stream_KER( this->addOutputPort( "MES_CDU_2_stream_KER") )
, MES_CDU_2_stream_OP( this->addOutputPort( "MES_CDU_2_stream_OP") )
, MES_CDU_2_stream_NA( this->addOutputPort( "MES_CDU_2_stream_NA") )
, MES_CDU_2_stream_VGO( this->addOutputPort( "MES_CDU_2_stream_VGO") )
, MES_CDU_2_stream_LD( this->addOutputPort( "MES_CDU_2_stream_LD") )
{

		
//	string tmpval1(MainSimulator::Instance().getParameter( this->description(), "MES_CDU_2_stream_LD_yield_val" ));
//	string tmpval2(MainSimulator::Instance().getParameter( this->description(), "MES_CDU_2_stream_VGO_yield_val" ));
//	string tmpval3(MainSimulator::Instance().getParameter( this->description(), "MES_CDU_2_stream_NA_yield_val" ));
//	string tmpval4(MainSimulator::Instance().getParameter( this->description(), "MES_CDU_2_stream_KER_yield_val" ));
//	string tmpval5(MainSimulator::Instance().getParameter( this->description(), "MES_CDU_2_stream_crude_yield_val" ));
//	string tmpval6(MainSimulator::Instance().getParameter( this->description(), "MES_CDU_2_stream_VR_yield_val" ));
//	MES_CDU_2_stream_LD_yield_val = str2float(tmpval1);
//	MES_CDU_2_stream_VGO_yield_val = str2float(tmpval2);
//	MES_CDU_2_stream_NA_yield_val = str2float(tmpval3);
//	MES_CDU_2_stream_KER_yield_val = str2float(tmpval4);
//	MES_CDU_2_stream_crude_yield_val = str2float(tmpval5);
//	MES_CDU_2_stream_VR_yield_val = str2float(tmpval6);
		
		
}


/*** the initFunction ***/
Model &MES_CDU_processing_2_model::initFunction()
{

		
	count = 0;
	delay = "00:00:00:00";
	passivate();
	return *this;
		
		
}

/*** the externalFunction ***/
Model &MES_CDU_processing_2_model::externalFunction( const ExternalMessage &msg )
{

			
	if(msg.port() == MES_CDU_2_stream_mode)
	{
		MES_CDU_2_stream_mode_val = msg.value();
		if(MES_CDU_2_stream_mode_val == 0)
		{
			MES_CDU_2_stream_crude_yield_val = 1.0;
			MES_CDU_2_stream_NA_yield_val = 0.04;
			MES_CDU_2_stream_KER_yield_val = 0.08;
			MES_CDU_2_stream_LD_yield_val = 0.24;
			MES_CDU_2_stream_VGO_yield_val = 0.16;
			MES_CDU_2_stream_VR_yield_val = 0.48;
		}
		else
		{
			MES_CDU_2_stream_crude_yield_val = 1.0;
			MES_CDU_2_stream_NA_yield_val = 0.05;
			MES_CDU_2_stream_KER_yield_val = 0.05;
			MES_CDU_2_stream_LD_yield_val = 0.25;
			MES_CDU_2_stream_VGO_yield_val = 0.15;
			MES_CDU_2_stream_VR_yield_val = 0.5;
		}
	}

	if( msg.port() == MES_CDU_2_stream_crude ){
		MES_CDU_2_stream_crude_flowrate = msg.value();
		count = 1;
		holdIn( active, delay);
	}
	return *this;
		
		
}

/*** the internalFunction ***/
Model &MES_CDU_processing_2_model::internalFunction( const InternalMessage &msg )
{

			
	passivate();
	return *this;
		
		
}

/*** the outputFunction ***/
Model &MES_CDU_processing_2_model::outputFunction( const InternalMessage &msg )
{

			
	if(count == 1)
	{
		count = 0;
		MES_CDU_2_stream_NA_flowrate = MES_CDU_2_stream_crude_flowrate * MES_CDU_2_stream_NA_yield_val;
		MES_CDU_2_stream_KER_flowrate = MES_CDU_2_stream_crude_flowrate * MES_CDU_2_stream_KER_yield_val;
		MES_CDU_2_stream_LD_flowrate = MES_CDU_2_stream_crude_flowrate * MES_CDU_2_stream_LD_yield_val;
		MES_CDU_2_stream_VGO_flowrate = MES_CDU_2_stream_crude_flowrate * MES_CDU_2_stream_VGO_yield_val;
		MES_CDU_2_stream_VR_flowrate = MES_CDU_2_stream_crude_flowrate * MES_CDU_2_stream_VR_yield_val;
		sendOutput( msg.time(), MES_CDU_2_stream_OP, MES_CDU_2_stream_crude_flowrate);
		sendOutput( msg.time(), MES_CDU_2_stream_NA, MES_CDU_2_stream_NA_flowrate);
		sendOutput( msg.time(), MES_CDU_2_stream_KER, MES_CDU_2_stream_KER_flowrate);
		sendOutput( msg.time(), MES_CDU_2_stream_VGO, MES_CDU_2_stream_VGO_flowrate);
		sendOutput( msg.time(), MES_CDU_2_stream_LD, MES_CDU_2_stream_LD_flowrate);
		sendOutput( msg.time(), MES_CDU_2_stream_VR, MES_CDU_2_stream_VR_flowrate);
	}

	return *this;
		
		
}

